package leetcodeHis.链表;

public class Algo_21_合并两个有序链表 {

    public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode result = new ListNode(-1);
        ListNode head = result;

        while (l1 != null || l2 != null) {
            if(l1 == null) { head.next = l2; break;}
            if(l2 == null) { head.next = l1; break;}

            if(l1.val <= l2.val) {
                head.next = new ListNode(l1.val);
                l1 = l1.next;
            }else {
                head.next = new ListNode(l2.val);
                l2 = l2.next;
            }
            head = head.next;
        }
        return result.next;
    }

    public static void main(String[] args) {
        ListNode a = new ListNode(1);
        a.next = new ListNode(2);
        a.next.next = new ListNode(3);

        ListNode b = new ListNode(1);
        b.next = new ListNode(2);
        b.next.next = new ListNode(3);

        ListNode merge = mergeTwoLists(a, b);
        while (merge!=null) {
            System.out.println(merge.val);
            merge = merge.next;
        }
    }
}
